package com.lg.cms.config;

/**
 * @Description: WebMvc配置类
 * @Author: lg
 * @create: 2020-09-02 21:32
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {
    // 使用私有属性加载拦截器, 解决拦截器内无法注入service问题

    @Value("${file.uri}")
    private String uploadUri;
    @Value("${file.path}")
    private String uploadPath;
    @Value("${template.uri}")
    private String templateUri;
    @Value("${template.path}")
    private String templatePath;
    /**
     * 默认试图注册
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/shouye");
        log.info("默认视图重定向初始化成功");
    }

    /**
     * 静态目录注册
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        log.info("资源映射初始化配置成功");
        log.info(uploadUri + "==" + uploadPath);
        log.info(templateUri + "==" + templatePath);
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler(uploadUri).addResourceLocations(uploadPath);
        registry.addResourceHandler(templateUri).addResourceLocations(templatePath);
    }

    /**
     * 拦截器注册
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册登录验证拦截器组件   addpath"/**" 拦截所有  excludepath除去 "login/"请求
//        registry.addInterceptor(adminLoginInterceptor)
//                .addPathPatterns("/super/**").excludePathPatterns(Arrays.asList(
//                "/super/login"));


//        log.info("拦截器:" + adminLoginInterceptor.toString() + "初始化成功");
    }


}
